package com.geeklan.smart.ui.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;

import com.geeklan.base.base.BaseActivity;
import com.geeklan.smart.R;
import com.geeklan.smart.common.Constants;
import com.geeklan.smart.data.model.User;
import com.geeklan.smart.data.repository.user.AccountRepository;
import com.geeklan.smart.databinding.ActivityRegisterBinding;
import com.geeklan.smart.ui.viewmodel.user.UserViewModel;
import com.geeklan.smart.ui.viewmodel.user.UserViewModelFactory;
import com.geeklan.smart.ui.web.WebViewActivity;
import com.geeklan.smart.utils.MD5Utils;
import com.geeklan.smart.utils.PasswordValidator;
import com.geeklan.smart.utils.SharedPreferencesUtil;

public class RegisterActivity extends BaseActivity<ActivityRegisterBinding> {
    private UserViewModel userViewModel;
    private boolean isPasswordVisible = false;
    private boolean isCheck = false;

    @Override
    protected ActivityRegisterBinding initBinding() {
        return ActivityRegisterBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        userViewModel = new ViewModelProvider(this, new UserViewModelFactory(new AccountRepository())).get(UserViewModel.class);

        userViewModel.getRegisterLiveData().observe(this, user -> {
            dismiss();

            Toast.makeText(this, user.getMsg(), Toast.LENGTH_SHORT).show();
            if(user.getCode() == 0) {
                LoginActivity.startLogin(this);
            }
        });

        userViewModel.getErrorLiveData().observe(this, s -> {
            dismiss();
            Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    protected void initView() {
        mBinding.tvLogin.setOnClickListener(v -> {
            if (isCheck) {
                if(TextUtils.isEmpty(mBinding.etInputAccount.getText().toString())){
                    Toast.makeText(this, R.string.login_input_account, Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!mBinding.etInputPwd.getText().toString().equals(mBinding.etInputPwdAgain.getText().toString())) {
                    Toast.makeText(this, R.string.again_input_pwd_error, Toast.LENGTH_SHORT).show();
                } else if(!PasswordValidator.isValidPassword(mBinding.etInputPwd.getText().toString())) {
                    Toast.makeText(this, R.string.input_password,Toast.LENGTH_SHORT).show();
                }else {
                    loading();
                    User user = new User();
                    user.setUsername(mBinding.etInputAccount.getText().toString());
                    user.setPassword(MD5Utils.encryptMD5(mBinding.etInputPwd.getText().toString() + "miaomojo"));

                    user.setIpAddress(SharedPreferencesUtil.getString("address_ip","0.0.0.0"));
                    user.setCity(SharedPreferencesUtil.getString("address_city","广东"));
                    user.setProvince(SharedPreferencesUtil.getString("address_province","深圳"));
                    userViewModel.register(user);
                }
            } else {
                Toast.makeText(this, R.string.Please_check_the_user_agreement, Toast.LENGTH_SHORT).show();
            }
        });

        mBinding.tvGoLogin.setOnClickListener(v -> {
            LoginActivity.startLogin(this);
        });

        mBinding.ivVisibility.setOnClickListener(v -> {
            if (isPasswordVisible) {
                // 隐藏密码
                mBinding.etInputPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                mBinding.etInputPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                mBinding.ivVisibility.setImageResource(R.drawable.login_pwd_hint); // 隐藏图标
            } else {
                // 显示密码
                mBinding.etInputPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                mBinding.etInputPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                mBinding.ivVisibility.setImageResource(R.drawable.login_pwd_show); // 显示图标
            }
            isPasswordVisible = !isPasswordVisible;

            // 将光标移动到末尾
            mBinding.etInputPwd.setSelection(mBinding.etInputPwd.getText().length());
            mBinding.etInputPwdAgain.setSelection(mBinding.etInputPwdAgain.getText().length());
        });

        mBinding.llCheck.setOnClickListener(v -> {
            if (isCheck) {
                isCheck = false;
                mBinding.ivCheck.setImageDrawable(ActivityCompat.getDrawable(this, R.drawable.ic_checkbox_def));
            } else {
                isCheck = true;
                mBinding.ivCheck.setImageDrawable(ActivityCompat.getDrawable(this, R.drawable.ic_checkbox_select));
            }
        });

        mBinding.tvPrivacy.setOnClickListener(v -> {
            WebViewActivity.startActivity(this, Constants.PRIVACY_URL);
        });

        mBinding.tvUser.setOnClickListener(v -> {
            WebViewActivity.startActivity(this, Constants.USER_URL);
        });
        mBinding.ivBack.setOnClickListener(v -> {
            finish();
        });
        mBinding.ivVisibility.setOnClickListener(v -> {
            if (!isShowPwd) {
                isShowPwd = true;
                mBinding.ivVisibility.setImageResource(R.drawable.login_pwd_show);
                //显示
                mBinding.etInputPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                mBinding.etInputPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                isShowPwd = false;
                mBinding.ivVisibility.setImageResource(R.drawable.login_pwd_hint);
                //隐藏
                mBinding.etInputPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                mBinding.etInputPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        });
    }

    private boolean isShowPwd = false;

    @Override
    protected void onApplyWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    @Override
    public void networkStatus(boolean isConnected) {

    }

    @Override
    public void networkType(int type) {

    }

    public static void startRegister(Context context) {
        Intent intent = new Intent(context, RegisterActivity.class);
        context.startActivity(intent);
    }
}